const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');

var app = getApp();
Page({
  data: {
    inputShowed: false,
    inputVal: "",//search value
    products: [],
    schoolList: [],
    param: {},
    endFlag: false,
    loadingFlag: false,
    regions: [],
    starRank: [
      { id: 5, name: '五星' },
      { id: 4, name: '四星' },
      { id: 3, name: '三星' },
      { id: 2, name: '二星' },
      { id: 1, name: '一星' }
    ],
    grade:[
      {id:1,name:'一级'},
      {id:2,name:'二级'},
      {id:3,name:'三级'}
    ],
    sort:[
      {id:1,name:'通过率'},
    ],
    sortId:0,
    starRankId:0,
    gradeId:0,
    activeRegionId: 0,
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
    regionId: 0,
    starRating: 0,
    showType: 3,
    totalPages: 1
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        //设置scroll-view 的高度
        var scollHeight = res.screenHeight - 90 - 65;
        that.setData({
          scollHeight: scollHeight,
          schoolList: []
        }
        );
        that.getRegions();
        that.getSchools();
      }
    })
  },

  getRegions: function () {
    var that = this;
    util.request(api.RegionList).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          regions: res.data.list
        });
      }

    });
  },

  changeRegion: function (e) {
    this.setData({
      regionId: e.currentTarget.id
    });
    this.getSchools();
  },

  changeStarRank:function (e) {
    this.setData({
      starRankId: e.currentTarget.id
    });
    this.getSchools();
  },
  changeGrade:function(e){
    this.setData({
      gradeId: e.currentTarget.id
    });
    //console.log(this.data.gradeId)
    this.getSchools();
  },
  changeSort:function(e){
    this.setData({
      sortId: e.currentTarget.id
    });
    //console.log(this.data.sortId)
    this.getSchools();
  },
  getSchools: function () {
    var that = this;
    //console.log(that.data.gradeId)
    util.request(api.SchoolList, {
      sortId:that.data.sortId,
      gradeId:that.data.gradeId,
      regionId: that.data.regionId,
      starRating: that.data.starRankId,
      type: that.data.showType,
      // page: that.data.page,
      // limit: that.data.limit
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          schoolList: res.data.list,
          totalPages: res.data.pages/*,
          endFlag: true*/
        });
      }
    });

  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getSchools();
    } else {
      // wx.showToast({
      //   title: '没有更多数据了',
      //   icon: 'none',
      //   duration: 2000
      // });
      this.setData({
        endFlag: true
      });
      return false;
    }
  },

  //事件处理函数
  bindViewTap: function (e) {
    var param = e.currentTarget.dataset;
    var id = param["id"];
    wx.navigateTo({
      url: '../detail/detail?id=' + id
    })
  },
  toNav: function (e) {
    var param = e.currentTarget.dataset;
    var latitude = param['latitude'];
    var longitude = param['longitude'];
    var name = param['name'];
    var address = param['address'];

    wx.openLocation({//?使用微信内置地图查看位置。
      latitude: Number(latitude),//要去地点的纬度
      longitude: Number(longitude),///要去地点的经度-地址
      name: name,//
      address: address
    })
  }
})